package com.xmtlxx.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.entity.SysCheckItem;
import com.xmtlxx.common.core.domain.entity.SysTagCheckitem;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xmtlxx.system.domain.bo.SysCheckItemBo;
import com.xmtlxx.system.domain.vo.SysCheckItemVo;
import com.xmtlxx.system.mapper.SysCheckItemMapper;
import com.xmtlxx.system.mapper.SysTagCheckitemMapper;
import com.xmtlxx.system.service.ISysCheckItemService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 检查项Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-04-26
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SysCheckItemServiceImpl implements ISysCheckItemService {

    private final SysCheckItemMapper baseMapper;

    private final SysTagCheckitemMapper sysTagCheckitemMapper;

    /**
     * 查询检查项
     *
     * @param itemId 检查项主键
     * @return 检查项
     */
    @Override
    public SysCheckItemVo queryById(Long itemId){
        return baseMapper.selectVoById(itemId);
    }

    /**
     * 查询检查项列表
     *
     * @param sysCheckItem 检查项
     * @return 检查项
     */
    @Override
    public TableDataInfo<SysCheckItemVo> queryPageList(SysCheckItem sysCheckItem, PageQuery pageQuery) {
//        LambdaQueryWrapper<SysCheckItem> lqw = buildQueryWrapper(bo);
//        Page<SysCheckItemVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
//        return TableDataInfo.build(result);
        return TableDataInfo.build(baseMapper.selectPageSysCheckItemList(pageQuery.build(),sysCheckItem));
    }

    /**
     * 查询检查项列表
     *
     * @param bo 检查项
     * @return 检查项
     */
    @Override
    public List<SysCheckItemVo> queryList(SysCheckItemBo bo) {
        LambdaQueryWrapper<SysCheckItem> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysCheckItem> buildQueryWrapper(SysCheckItemBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysCheckItem> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), SysCheckItem::getName, bo.getName());
        lqw.eq(bo.getTypeId() != null, SysCheckItem::getTypeId, bo.getTypeId());
        lqw.eq(StringUtils.isNotBlank(bo.getValueType()), SysCheckItem::getValueType, bo.getValueType());
        lqw.eq(StringUtils.isNotBlank(bo.getHtmlType()), SysCheckItem::getHtmlType, bo.getHtmlType());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysCheckItem::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getItemType()), SysCheckItem::getItemType, bo.getItemType());
        lqw.eq(StringUtils.isNotBlank(bo.getIsMajor()), SysCheckItem::getIsMajor, bo.getIsMajor());
        lqw.orderByAsc(SysCheckItem::getSort);
        return lqw;
    }

    /**
     * 新增检查项
     *
     * @param bo 检查项
     * @return 结果
     */
    @Override
    public Boolean insertByBo(SysCheckItemBo bo,String[] tagIds) {
        SysCheckItem add = BeanUtil.toBean(bo, SysCheckItem.class);
        validEntityBeforeSave(add);

        //填充UUID
//        add.setItemId(IdUtil.simpleUUID());

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setItemId(add.getItemId());
        }

        if(tagIds != null && tagIds.length > 0){
            for(String id : tagIds){
                SysTagCheckitem tagCheckitem = new SysTagCheckitem();
                tagCheckitem.setTagId(id);
                tagCheckitem.setItemId(bo.getItemId());
                tagCheckitem.setFlag("item");
                tagCheckitem.setTid(IdUtil.simpleUUID());
                sysTagCheckitemMapper.insert(tagCheckitem);
            }
        }

        return flag;
    }

    /**
     * 修改检查项
     *
     * @param bo 检查项
     * @return 结果
     */
    @Override
    public Boolean updateByBo(SysCheckItemBo bo,String[] tagIds) {
        SysCheckItem update = BeanUtil.toBean(bo, SysCheckItem.class);
        validEntityBeforeSave(update);

        LambdaQueryWrapper<SysTagCheckitem> lqw = Wrappers.lambdaQuery();
        lqw.eq(SysTagCheckitem::getItemId,bo.getItemId());
        sysTagCheckitemMapper.delete(lqw);

        if(tagIds != null && tagIds.length > 0){
            for(String id : tagIds){
                SysTagCheckitem tagCheckitem = new SysTagCheckitem();
                tagCheckitem.setTagId(id);
                tagCheckitem.setItemId(bo.getItemId());
                tagCheckitem.setFlag("item");
                tagCheckitem.setTid(IdUtil.simpleUUID());
                sysTagCheckitemMapper.insert(tagCheckitem);
            }
        }

        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysCheckItem entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除检查项
     *
     * @param ids 需要删除的检查项主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

}
